//
//  XS_KCTeachers.swift
//  XueDao
//
//  Created by 韩云智 on 2020/12/29.
//

import UIKit
import RxCocoa
import RxSwift
import RxDataSources

class XS_KCTeachers: UIView {
    lazy var disposeBag = DisposeBag()
    lazy var modelPS = PublishSubject<[XS_KCteacherBOListM]>()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        upView()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func upView() {
        backgroundColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: .scale*146, height: .scale*80)
        flowLayout.scrollDirection = .horizontal
        let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collection.showsHorizontalScrollIndicator = false
        collection.layer.backgroundColor = #colorLiteral(red: 0.968627451, green: 0.968627451, blue: 0.968627451, alpha: 1)
        collection.layer.cornerRadius = .scale*4
        collection.bounces = false
        addSubview(collection)
        collection.snp.makeConstraints {
            $0.left.top.equalTo(.scale*16)
            $0.right.equalTo(-.scale*16)
            $0.height.equalTo(.scale*80)
            $0.bottom.lessThanOrEqualTo(-.scale*16)
        }
        
        collection.register(XS_KCTeacherCell.self, forCellWithReuseIdentifier: "cell")
        let dataSource = RxCollectionViewSectionedReloadDataSource<SectionModel<String, XS_KCteacherBOListM>>(configureCell: { ds, cv, ip, it in
            let cell = cv.dequeueReusableCell(withReuseIdentifier: "cell", for: ip) as! XS_KCTeacherCell
            cell.modelPS.onNext(it)
            return cell
        })
        
        modelPS.asDriver(onErrorJustReturn: [])
            .flatMapLatest({ Driver.just([SectionModel<String, XS_KCteacherBOListM>(model: "", items: $0)]) })
            .drive(collection.rx.items(dataSource: dataSource))
            .disposed(by: disposeBag)
    }
}

class XS_KCTeacherCell: XS_BaseCCell {
    lazy var modelPS = PublishSubject<XS_KCteacherBOListM>()
    override func upView() {
        let imgV = UIImageView()
        imgV.layer.cornerRadius = .scale*28
        imgV.layer.masksToBounds = true
        contentView.addSubview(imgV)
        imgV.snp.makeConstraints({
            $0.width.height.equalTo(.scale*56)
            $0.left.equalTo(.scale*12)
            $0.centerY.equalToSuperview()
        })
        let nameLab = UILabel.xsLabel(size: .scale*16, fontName: .medium, color: #colorLiteral(red: 0.2196078431, green: 0.2352941176, blue: 0.2470588235, alpha: 1))
        contentView.addSubview(nameLab)
        nameLab.snp.makeConstraints({
            $0.top.equalTo(.scale*16)
            $0.left.equalTo(imgV.snp.right).offset(.scale*10)
            $0.right.lessThanOrEqualTo(-.scale*10)
        })
        let tagLab = UILabel.xsLabel(size: .scale*14, fontName: .regular, color: #colorLiteral(red: 0.4470588235, green: 0.4666666667, blue: 0.4862745098, alpha: 1))
        contentView.addSubview(tagLab)
        tagLab.snp.makeConstraints({
            $0.top.equalTo(nameLab.snp.bottom).offset(.scale*6)
            $0.left.equalTo(imgV.snp.right).offset(.scale*10)
            $0.right.lessThanOrEqualTo(-.scale*10)
        })
        
        modelPS.subscribe(onNext: { item in
            imgV.sd_setImage(with: URL(string: item.imgUrl))
            nameLab.text = item.name
            tagLab.text = item.title
        }).disposed(by: disposeBag)
    }
}
